//
//  MyServiceView.m
//  PigMarket
//
//  Created by Felix on 2019/3/24.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MyServiceView.h"

#import "MyServiceCellFlowLayout.h"
#import "MyServiceCell.h"

@interface MyServiceView ()<UICollectionViewDataSource,UICollectionViewDelegate>

/**
 title
 */
@property (nonatomic , strong) UILabel *title;

/**
 line
 */
@property (nonatomic , strong) UILabel *line;

/**
 serviceCollectionView
 */
@property (nonatomic , strong) UICollectionView *serviceCollectionView;

/**
 serviceData
 */
@property (nonatomic , strong) NSArray *serviceData;

@end

@implementation MyServiceView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.serviceData = @[@{@"title" : @"店铺收藏", @"icon" : @"like_collection_selected"},
                             @{@"title" : @"豚券", @"icon" : @"user_service_coupon"},
                             @{@"title" : @"我的资产", @"icon" : @"user_service_asset"},
                             @{@"title" : @"我的领养", @"icon" : @"user_service_myadopted"},
                             @{@"title" : @"购物车", @"icon" : @"user_service_shoppingcart"},
                             @{@"title" : @"收货地址", @"icon" : @"address_tip_green"},
                             @{@"title" : @"常见问题", @"icon" : @"user_service_query"},
                             @{@"title" : @"帮助", @"icon" : @"user_service_help"},
                             @{@"title" : @"商家入驻", @"icon" : @"user_service_vendorenter"}];
        
        
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.title];
    [self addSubview:self.line];
    [self addSubview:self.serviceCollectionView];
}

- (void)initLayout {
    // 布局子控件
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.title.mas_bottom).offset(12);
        make.height.mas_equalTo(1);
    }];
    [self.serviceCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(240);
        make.top.mas_equalTo(self.line.mas_bottom);
    }];
}

#pragma mark - delegate
#pragma mark UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.serviceData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyServiceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyServiceCellID" forIndexPath:indexPath];
    
    cell.cellData = self.serviceData[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.myServiceBlock) {
        self.myServiceBlock(indexPath.row);
    }
}


#pragma mark - getter and setter

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor colorWithHexString:@"#262626"];
        _title.font = [UIFont middleFont];
        _title.text = @"我的服务";
    }
    return _title;
}
- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor dividerColor];
    }
    return _line;
}
- (UICollectionView *)serviceCollectionView {
    if (!_serviceCollectionView) {
        MyServiceCellFlowLayout *flowLayout = [[MyServiceCellFlowLayout alloc] init];
        _serviceCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        // 注册CollectionView的Cell
        [_serviceCollectionView registerClass:[MyServiceCell class] forCellWithReuseIdentifier:@"MyServiceCellID"];
        _serviceCollectionView.delegate = self;
        _serviceCollectionView.dataSource = self;
        _serviceCollectionView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return _serviceCollectionView;
}
- (NSArray *)serviceData {
    if (!_serviceData) {
        _serviceData = [NSArray array];
    }
    return _serviceData;
}

@end
